<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温度转换器</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f2f2f2;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        h1 {
            margin-bottom: 20px;
        }

        input {
            padding: 10px;
            width: 80%;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        button {
            padding: 10px 20px;
            border: none;
            background-color: #4CAF50;
            color: white;
            font-size: 16px;
            cursor: pointer;
            border-radius: 5px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            color: #333;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>温度转换器</h1>

        <!-- 用户输入摄氏度 -->
        <input type="number" id="celsius" placeholder="请输入摄氏度" />

        <!-- 转换按钮 -->
        <button id="convertBtn">转换为华氏度</button>

        <!-- 显示转换后的结果 -->
        <div class="result" id="result"></div>
    </div>

    <script>
        // 摄氏度转华氏度的转换函数
        const celsiusToFahrenheit = degree => 1.8 * degree + 32;

        // 获取DOM元素
        const celsiusInput = document.getElementById('celsius');
        const convertButton = document.getElementById('convertBtn');
        const resultDiv = document.getElementById('result');

        // 点击按钮时触发的事件
        convertButton.addEventListener('click', () => {
            const celsius = parseFloat(celsiusInput.value);

            // 确保输入的是有效数字
            if (!isNaN(celsius)) {
                const fahrenheit = celsiusToFahrenheit(celsius);
                resultDiv.textContent = `${celsius}°C = ${fahrenheit.toFixed(2)}°F`;  // 显示结果并保留两位小数
            } else {
                resultDiv.textContent = "请输入有效的摄氏度值";  // 提示错误信息
            }
        });
    </script>

</body>

</html>